//
//  BaodanViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/12/23.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "BaodanViewController.h"

@interface BaodanViewController ()<WKNavigationDelegate,WKUIDelegate>

@property (nonatomic, strong) NSMutableArray *array;

@end

@implementation BaodanViewController

- (void)dealloc{

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    self.title = @"我的保单";
    [self request_baodan];
}


- (void)initView {
    
    self.view.backgroundColor = UIColorHex(0xf1f1f1);
    [self.view addSubview:self.webview];

    [self.webview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.mas_equalTo(0);
    }];
    
}

#pragma mark VR网页初始化
- (WKWebView *)webview {
    if (!_webview) {
        
        WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
        WKUserContentController *userController = [[WKUserContentController alloc] init];
        configuration.userContentController = userController;
        
        _webview = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
        _webview.navigationDelegate = self;
        _webview.UIDelegate = self;
        _webview.backgroundColor = UIColorHex(0xf1f1f1);
                
    }
    return _webview;
}

- (NSString *)reSizeImageWithHTML:(NSString *)html {
    return [NSString stringWithFormat:@"<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0'><meta name='apple-mobile-web-app-capable' content='yes'><meta name='apple-mobile-web-app-status-bar-style' content='black'><meta name='format-detection' content='telephone=no'><style type='text/css'>img{width:%@}*{padding:0, margin:0}*{white-space: pre-line}</style>%@",@"100%",html];
}

- (void)request_baodan {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];

    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_UserInsurance];
    
    WEAKSELF
    [SVProgressHUD showWithStatus:@"正在请求保单"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {

        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                
                NSDictionary *resultDic = [data valueForKey:@"data"];
                NSString *insuranceUrl = [resultDic valueForKey:@"insuranceUrl"];
                
                if (insuranceUrl.length > 0) {
                    weakSelf.array = @[].mutableCopy;
                    [weakSelf.array addObject:insuranceUrl];
                    NSString *htmlString = [Utilities fixHtmlStringWithUrlArray:self.array text:@""];
                    htmlString = [weakSelf reSizeImageWithHTML:htmlString];
                    if (htmlString.length > 0) {
                        [weakSelf.webview loadHTMLString:htmlString baseURL:nil];
                    }
                } else {
                    
                    [[TTNoResultManager shareManager] showNoResultWithStateInView:weakSelf.webview errorCode:kTTViewState_NoBaoDan btnTitle:@"去问问我的统筹师" didPressButton:^{
                       
                        [[TT_IMContactManager manager] ImContactWithType:TT_IMContactType_tongchoushi];
                    }];
                }
            }
        }
    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
    }];
    
}

@end
